from pyNastran.converters.nastran.nastran_to_vtk import nastran_to_vtk
import meshio
import vtk

# op2_filename = 'solid-shell-static.op2'
# vtu_filename = 'solid-shell-static.vtu'
# vtk_filename = 'solid-shell-static.vtk'
# bdf_filename = 'solid-shell-static.bdf'
# msh_filename = 'solid-shell-static.msh'
def op2vtk(bdf_filename, op2_filename, vtk_filename):
    nastran_to_vtk(bdf_filename, op2_filename, vtu_filename)
    # 读取VTU文件
    reader = vtk.vtkXMLUnstructuredGridReader()
    reader.SetFileName(vtu_filename)
    reader.Update()
    # 写入VTK文件   
    writer = vtk.vtkUnstructuredGridWriter()
    writer.SetFileName(vtk_filename)
    writer.SetInputConnection(reader.GetOutputPort())
    writer.SetFileTypeToBinary()  # 或SetFileTypeToASCII  SetFileTypeToBinary()
    writer.Write()

def op2msh(bdf_filename, op2_filename, msh_filename):
    # 读取 .vtk 文件
    vtk_filename=op2_filename.replace(".op2",".vtk")
    op2vtk(bdf_filename, op2_filename, vtk_filename)
    mesh = meshio.read(vtk_filename)
    meshio.write(msh_filename, mesh, file_format="gmsh22", binary=False)
# dim_tags = []
# geom_tags = []
# phys_tags=[]
# for cell_block in mesh.cells:
#     cell_type = cell_block.type
#     # Assign dimension based on cell type
#     if cell_type in ["vertex"]:  # 0D
#         dim = 0
#         tag=1
#         dim_tag=(dim,tag)
#         phys_tag = np.full(len(cell_block.data), tag, dtype=int)  # 物理组标签 1
#         geom_tag = np.full(len(cell_block.data), tag, dtype=int)  # 几何实体标签 1
#     elif cell_type in ["line"]:  # 1D
#         dim = 1
#         tag=2
#         dim_tag=(dim,tag)
#         phys_tag = np.full(len(cell_block.data), tag, dtype=int)  # 物理组标签 1
#         geom_tag = np.full(len(cell_block.data), tag, dtype=int)  # 几何实体标签 1
#     elif cell_type in ["triangle", "quad"]:  # 2D
#         dim = 2
#         tag=3
#         dim_tag=(dim,tag)
#         phys_tag = np.full(len(cell_block.data), tag, dtype=int)  # 物理组标签 1
#         geom_tag = np.full(len(cell_block.data), tag, dtype=int)  # 几何实体标签 1
#     elif cell_type in ["tetra", "hexahedron", "wedge", "pyramid"]:  # 3D
#         dim = 3
#         tag=4
#         dim_tag=(dim,tag)
#         phys_tag = np.full(len(cell_block.data), tag, dtype=int)  # 物理组标签 1
#         geom_tag = np.full(len(cell_block.data), tag, dtype=int)  # 几何实体标签 1
#     else:
#         raise ValueError(f"Unsupported cell type: {cell_type}")

#     # Assign a unique tag (e.g., 1) for each cell type
#     # You can customize tags for specific entities if needed
#     dim_tags.append(dim_tag)
#     geom_tags.append(geom_tag)
#     phys_tags.append(phys_tag)  # Default physical tag
#     # phys_tags=np.full(len(cell_block.data), 2, dtype=int)

# # Add dim_tags to point_data
# mesh.point_data["gmsh:dim_tags"] = dim_tags
# mesh.cell_data["gmsh:geometrical"] = geom_tags
# mesh.cell_data["gmsh:physical"] = phys_tags
# meshio.write(msh_filename, mesh, file_format="gmsh22")
# 创建新的 Mesh 对象，仅包含点和单元信息
# mesh_no_data = meshio.Mesh(
#     points=mesh.points,
#     cells=mesh.cells,
#     cell_data=mesh.cell_data,
#     point_data=mesh.point_data,
# )


